<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <script src="../../layui/layui.js"></script>
    <script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
</head>

<body>
    <div class="layui-container">
        <div class="layui-row">
            <div class="layui-col-lg-offset3" layui-col-lg6>
                <div class="layui-form layuimini-form">
                    <form class="layui-form" action="" lay-filter="userForm">
                        <div class="layui-form-item">
                            <label class="layui-form-label">保安姓名：</label>
                            <div class="layui-input-block">
                                <input type="text" name="name" required lay-verify="required" placeholder="请输入用户名"
                                    autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <input type="hidden" name="id" value="" class="layui-input">


                        <!-- <div class="layui-form-item">
                            <label class="layui-form-label">密码</label>
                            <div class="layui-input-inline">
                                <input type="password" name="password" required lay-verify="required"
                                    placeholder="请输入密码" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-form-mid layui-word-aux">密码不能为空</div>
                        </div> -->

                        <div class="layui-form-item">
                            <label class="layui-form-label">性别：</label>
                            <div class="layui-input-block">
                                <input type="radio" name="sec" value="男" title="男" checked>
                                <input type="radio" name="sec" value="女" title="女">
                            </div>
                        </div>

                     

                
                        <div class="layui-form-item">
                            <label class="layui-form-label">年龄: </label>
                            <div class="layui-input-block">
                                <input type="text" name="age" required lay-verify="required" placeholder="请输入年龄"
                                    autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">工资：</label>
                            <div class="layui-input-block">
                                <input type="text" name="money" required lay-verify="required" placeholder="请输入工资"
                                    autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">职位：</label>
                            <div class="layui-input-block">
                                <input type="text" name="job" required lay-verify="required" placeholder="请输入手机号"
                                    autocomplete="off" class="layui-input">
                            </div>
                        </div>


                        <div class="layui-form-item">
                            <label class="layui-form-label">照片：</label>
                            <input type="hidden" name="pic" id="pic">
                            <div class="layui-input-block">
                                <button type="button" class="layui-btn" id="durl">
                                    <i class="layui-icon">&#xe67c;</i>上传图片
                                </button>

                                <div class="layui-upload-list">
                                    <img style="width: 100px" class="layui-upload-img" id="demo1">
                                    <p id="demoText"></p>
                                </div>

                                <div style="width: 95px;">
                                    <div class="layui-progress layui-progress-big" lay-showpercent="yes"
                                        lay-filter="demo">
                                        <div class="layui-progress-bar" lay-percent=""></div>
                                    </div>
                                </div>
                            </div>
                        </div>


                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>
                    </form>
                </div>

            </div>
        </div>
    </div>


    <script>
        //Demo
        layui.use(['form', 'upload', 'element'], function () {
            // 表单对象  form
            var form = layui.form
                //jquery 对象 $
                , $ = layui.jquery
                , element = layui.element
                , upload = layui.upload
                , layer = layui.layer

            // 从 localStorage 把数据取出来   并转成 js 对象
            //  JSON.parse("str")   把字符串转为 js 对象
            var data = JSON.parse(localStorage.getItem("doctor"));

            //给表单数据赋值
            form.val("userForm", {
                //给隐藏域的id赋值 方便根据id编辑用户
                "id": data.id,
                "name": data.name,
                "sex": data.sex,
                "age": data.age,
                "money": data.money,
                "job": data.job,
                // "department": data.department,
            });

            // 图片回显
            $("#demo1").attr("src", data.pic);
            $("#pic").val(data.pic);


            //执行实例
            var uploadInst = upload.render({
                elem: '#durl' //绑定元素
                , field: "file"
                , url: 'http://localhost:8080/san/upload' //上传接口
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#demo1').attr('src', result); //图片链接（base64）
                    });

                    element.progress('demo', '0%'); //进度条复位
                    layer.msg('上传中', { icon: 16, time: 0 });
                }
                , done: function (res) {

                    console.log(res);
                    //上传完毕回调
                    if (res.code == 0) {
                        $("#demo1").attr("src", res.map.url);
                        $("#pic").val(res.map.url);  //把图片地址 写到隐藏域 上传的时候 传到后台
                    }

                    $('#demoText').html(''); //置空上传失败的状态

                }
                , error: function () {
                    //请求异常回调
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });

                }
                //进度条
                , progress: function (n, elem, e) {
                    element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
                    if (n == 100) {
                        layer.msg('上传完毕', { icon: 1 });
                    }
                }
            });

            //监听提交
            form.on('submit(formDemo)', function (data) {
                console.log(data);
                $.ajax({
                    "url": "http://localhost:8080/rears/updataRear",
                    "data": data.field,
                    // result 参数 表式后台响应的数据
                    "success": function (result) {
                        if (result.code == 1) {

                            layer.alert("修改成功", function () {
                                // 关闭 弹出层
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                            })
                        } else {
                            layer.msg(result.msg);
                        }
                    },
                    "dataType": "json"
                });

                return false;
            });
        });
    </script>

</body>
</body>

</html>